﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using Cinch;
using SLS.ExClassLib.DataType;
using SLS.ExClassLib.Database;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.Data;
namespace SLS.Base
{
    public partial class SLBC
    {
        public string FindFistLess(List<string> lstSource, string target)
        {
            string result = target;
            List<string> lstless = new List<string>();
            try
            {
                if (lstSource.Count() <= 1)
                    return target;
                foreach (string x in lstSource)
                {
                    if (System.Convert.ToInt16(x) < System.Convert.ToInt16(target))
                    {
                        lstless.Add(x);
                    }
                }
                lstless.Sort();
                int count = lstless.Count();
                result = lstless[count - 1];
            }
            catch (Exception ex)
            {
                Tracer.Error(this.GetType().ToString(), ex);
            }
            return result;
        }
        public virtual void Load5ExchangeDataSet(Dictionary<string, object> m_DictBaseInit)
        {
            try
            {
                if (m_DictBaseInit != null)
                {
                    if (!m_DictBaseInit.ContainsKey("ExChangeDataSet"))
                        return;
                    ExChangeDataSet = (DataSet)m_DictBaseInit["ExChangeDataSet"];
                    if (ExChangeDataSet != null)
                    {
                        foreach (DataTable tbTemp in ExChangeDataSet.Tables)
                        {
                            string TBName = tbTemp.TableName;
                            #region Merge Data into the UI
                            if (TBName.Contains("QueryCond"))
                            {
                                SetEnv.Tables[TBName].Clear();
                                SetEnv.Merge(tbTemp);
                                continue;
                            }
                            ExDataRow exd = new ExDataRow();
                            foreach (DataRow dwTemp in tbTemp.Rows)
                            {
                                exd.GetExDataRow(dwTemp, true);
                            } 
                            #endregion
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        } 
        public virtual void SetDefaultActiveRecord(DataSet setEvent = null)
        {
            try
            {
                TableBase.Clear();
                string INSTANCEID = "";
                DataRow dwTemp;
                if (setEvent != null)
                {
                    foreach (DataRow dwEvent in setEvent.Tables[0].Rows)
                    {
                        ExDataRow exd = new ExDataRow();
                        exd.GetExDataRow(dwEvent, true);
                        INSTANCEID = dwEvent["INSTANCEID"].ToString();
                        var query = from ExDataRow datalist in DataAccessService.Instance.DataList
                                    where datalist.INSTANCEID == INSTANCEID
                                    select datalist;
                        foreach (ExDataRow exRow in query)
                        {
                            dwTemp = TableBase.NewRow();
                            dwTemp.ItemArray = exRow.Item.ItemArray;
                            TableBase.Rows.Add(dwTemp);
                        }
                    }
                    return;
                }
                flagActiveRecordAlreadySettled = true;
                foreach (ExDataRow exRow in DataAccessService.Instance.DataList)
                {
                    dwTemp = TableBase.NewRow();
                    dwTemp.ItemArray = exRow.Item.ItemArray;
                    TableBase.Rows.Add(dwTemp);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public void cSender_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            ComboBox cSender = (ComboBox)sender;
            cSender.IsDropDownOpen = true;
        }
        public virtual void OnDropDownOpened(object sender, EventArgs e)
        {
            try
            {
                ComboBox cmb=sender as ComboBox;
                string tbName = ((System.Data.DataView)((System.Windows.Data.BindingListCollectionView)cmb.ItemsSource).SourceCollection).Table.TableName;
                if (ViewModelBase != null)
                {
                    if (ViewModelBase.ActiveExDataRow != null)
                    {
                        if (ViewModelBase.ActiveExDataRow.TBName == tbName)
                            return;
                        ViewModelBase.ActiveExDataRow.BuildSet4Reference(tbName);
                    }
                } 
            }
            catch (Exception ex)
            {
                throw;
            } 
        }   
        public virtual void ValidateGDKEY(DataRow dwThis)
        {
            try
            {

            }
            catch (Exception ex)
            {
                Tracer.Error(this.Address + ex.StackTrace, ex);
            }
        } 
        public virtual void ItemsControls_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            try
            {
                ItemsControl sd = sender as ItemsControl;
                ICollectionView view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                DataRowView drv;
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    if (drv != null)
                    {
                        //this.ActiveDataRow = drv.Row;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error("ZoomDocument" + this.Name + ".ItemsControls_SelectionChanged", ex);
            }
        }
    }
}